package com.kehutong.admin.controler;

import com.kehutong.admin.entity.BusinessTag;
import com.kehutong.admin.service.BusinessTagService;
import com.kehutong.common.entity.Root;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.util.Objects;
import org.coraframework.util.UUIDGenerator;

import java.io.IOException;
import java.util.List;

/**
 * 业务标签
 * @author: liukunlong
 * @date: 2021-07-28 11:52
 */
@MvcAction
@ReqMapping("/admin/sys/business/tag")
public class BusinessTagController {

    static final Logger logger = LoggerFactory.getLogger(BusinessTagService.class);

    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private BusinessTagService businessTagService;

    /**
     * 根据条件获取标签分页列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @HasPermission("sys:business:tag:view")
    @ReqMapping("/page")
    public Page<BusinessTag> page(JSONObject jsonObject) throws Exception {
        return businessTagService.page(jsonObject);
    }


//    /**
//     * 根据条件获取标签列表
//     *
//     * @param jsonObject 查询对象
//     * @return 住户汽车列表
//     * @throws IOException 操作异常
//     */
//    @HasPermission("sys:business:tag:view")
    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) throws Exception {
        return businessTagService.list(jsonObject);
    }


    @HasPermission("sys:business:tag:view")
    @ReqMapping("/get")
    public Object get(BusinessTag businessTagGroup) {
        return businessTagService.get(businessTagGroup);
    }


    @HasPermission("sys:business:tag:edit")
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(BusinessTag businessTag) {
        return businessTagService.save(businessTag);
    }


    @HasPermission("sys:business:tag:edit")
    @Bean(copy = true)
    @ReqMapping("/update")
    public Object update(BusinessTag businessTag) throws Exception {
        return businessTagService.update(businessTag);
    }


    @HasPermission("sys:business:tag:edit")
    @ReqMapping("/delete")
    public Object delete(BusinessTag businessTag) {
        return businessTagService.delete(businessTag);
    }

    /**
     * 根据条件获取标签组列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @HasPermission("sys:business:tag:view")
    @ReqMapping("/group/page")
    public Page<BusinessTag> groupPage(JSONObject jsonObject) throws Exception {
        return businessTagService.groupPage(jsonObject);
    }


    @HasPermission("sys:business:tag:view")
    @ReqMapping("/group/list")
    public Object groupList(JSONObject jsonObject) throws Exception {
        return businessTagService.groupList(jsonObject);
    }


    @HasPermission("sys:business:tag:view")
    @ReqMapping("/group/get")
    public Object groupGet(BusinessTag businessTagGroup) {
        return businessTagService.groupGet(businessTagGroup);
    }


    @HasPermission("sys:business:tag:edit")
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/group/save")
    public Object groupSave(JSONObject jsonObject) {
        String name=jsonObject.getString("name");
        JSONArray jsonArray=jsonObject.getJSONArray("childList");
        BusinessTag businessTagGroup = Pool.newInstance(BusinessTag.class, UUIDGenerator.randomUUID());
        businessTagGroup.setPid(Root.ROOT_ID);
        businessTagGroup.setGroup(true);
        businessTagGroup.setName(name);
        businessTagGroup.setCreateBy(Root.ROOT_ID);
        businessTagGroup.setCreateName("系统");
        businessTagGroup.setSort(1);
        List<BusinessTag> childList = JSONObject.parseArray(jsonArray.toJSONString(), BusinessTag.class);
        businessTagGroup.setChildList(childList);
        return businessTagService.groupSave(businessTagGroup);
    }


    @HasPermission("sys:business:tag:edit")
    @Bean(copy = true)
    @ReqMapping("/group/update")
    public Object groupUpdate(JSONObject jsonObject) throws Exception {
        JSONArray jsonArray = jsonObject.getJSONArray("childList");
        if(Objects.nonEmpty(jsonArray)){
            for (Object tagJsonObj : jsonArray) {
                JSONObject tagJson = (JSONObject) tagJsonObj;
                tagJson.remove("type");
            }
        }
        jsonObject.remove("childList");
        jsonObject.remove("type");
        BusinessTag businessTagGroup=JSONObject.parseObject(jsonObject.toJSONString(),BusinessTag.class);
        List<BusinessTag> childList = JSONObject.parseArray(jsonArray.toJSONString(), BusinessTag.class);
        businessTagGroup.setChildList(childList);
        return businessTagService.groupUpdate(businessTagGroup);
    }


    @HasPermission("sys:business:tag:edit")
    @ReqMapping("/group/delete")
    public Object groupDelete(BusinessTag businessTagGroup) {
        return businessTagService.groupDelete(businessTagGroup);
    }
}
